// 用户登录注册
var express = require('express');
var router = express.Router();
const sql = require('../conf/sql')
const pool = require('../conf/db')
/* GET users listing. */
// 用户注册
router.post('/register', function (req, res, next) {
  
  pool.query(sql.queryUserByUaccount, [req.body.uaccount], (err, result) => {
    if (err) {
      next(err)
      return;
    }
    if (result.length != 0) {
      res.json({ code: 201, msg: '用户名已被注册' })
      return;
    } else {
      pool.query(sql.userRegister, [req.body], (err, result) => {
        if (err) {
          next(err)
          return;
        }
        console.log(result);
        res.json({ code: 200, msg: '注册成功' });
      })
    }
  })
});
// 用户登录
router.post('/login', function (req, res, next) {
  pool.query(sql.userLogin, [req.body.uaccount, req.body.upwd], (err, result) => {
    if (err) {
      next(err)
      return;
    }
    console.log(result);
    if (result.length == 0) {
      res.json({ code: 201, msg: '登录失败' });
      return;
    } else {
      res.json({ code: 200, msg: '登录成功' });

    }
  })
});

module.exports = router;